# import win32print,win32api,os
# class devices:
#     def __init__(self):
#         # self.support=[{'type':'printer','name':'Microsoft Print to PDF','status':True}]
#         self.support=[{'type':'printer','name':'HP Deskjet 2520 series','status':True}]

#     # 调用该函数检查设备是否在线
#     def checkSupport(self):
#         for i in range(0,len(self.support)):
#             if self.support[i]['type']=='printer':
#                 self.support[i]['status']=self.checkPrinter(self.support[i]['name'])
#     def checkPrinter(self,name):
#         printers = self.get_printer_list_with_status()
#         for printer in printers:
#             if printer['name']==name and printer['is_online'] is True:
#                 win32print.SetDefaultPrinter(name)
#                 return True
#         return False
#     # 调用该函数执行打印任务
#     def print(self,path=r'F:\p2p_v2.0\p2p-chat-software-pc-version-2\backend\P2P\print.pdf'):
#         if self.support[0]['status'] is True:
#             win32api.ShellExecute(0, "print", path, None, ".", 0)
#             return True
#         return False

#     def get_printer_list_with_status(self):
#         printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS)
#         printer_list = []
#         for printer in printers:
#             printer_name = printer[2]
#             try:
#                 # 打开打印机获取状态
#                 handle = win32print.OpenPrinter(printer_name)
#                 attributes = win32print.GetPrinter(handle, 2)['Attributes']
#                 status = win32print.GetPrinter(handle, 2)['Status']
#                 win32print.ClosePrinter(handle)
#                 # 检查是否离线
#                 is_offline = bool(attributes & win32print.PRINTER_ATTRIBUTE_WORK_OFFLINE)
#                 printer_list.append({
#                     "name": printer_name,
#                     "is_online": not is_offline,
#                     "status": "Online" if not is_offline else "Offline"
#                 })
#             except Exception as e:
#                 printer_list.append({
#                     "name": printer_name,
#                     "is_online": False,
#                     "status": f"Error: {e}"
#                 })
#         return printer_list

# if __name__ == "__main__":    
#     test=devices()
#     # print(test.get_printer_list_with_status())
#     # print(os.path.exists("print.pdf"))  # 检查文件是否存在
#     test.checkSupport()
#     print(test.support)

#     # test.print()
import win32print,win32api
class devices:
    def __init__(self):
        self.support=[{'type':'printer','name':'HP Deskjet 2520 series','status':True}]
    # 调用该函数检查设备是否在线
    def checkSupport(self):
        for i in range(0,len(self.support)):
            if self.support[i]['type']=='printer':
                self.support[i]['status']=self.checkPrinter(self.support[i]['name'])
        return self.support
    def checkPrinter(self,name):
        printers = self.get_printer_list_with_status()
        for printer in printers:
            if printer['name']==name and printer['is_online'] is True:
                win32print.SetDefaultPrinter(name)
                return True
        return False
    # 调用该函数执行打印任务
    def print(self,path=r'F:\p2p_v2.0\p2p-chat-software-pc-version-2\backend\P2P\print.pdf'):
        if self.support[0]['status'] is True:
            win32api.ShellExecute(0, "print", path, None, ".", 0)
            return True
        return False

    def get_printer_list_with_status(self):
        printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS)
        printer_list = []
        for printer in printers:
            printer_name = printer[2]
            try:
                # 打开打印机获取状态
                handle = win32print.OpenPrinter(printer_name)
                attributes = win32print.GetPrinter(handle, 2)['Attributes']
                status = win32print.GetPrinter(handle, 2)['Status']
                win32print.ClosePrinter(handle)
                # 检查是否离线
                is_offline = bool(attributes & win32print.PRINTER_ATTRIBUTE_WORK_OFFLINE)
                printer_list.append({
                    "name": printer_name,
                    "is_online": not is_offline,
                    "status": "Online" if not is_offline else "Offline"
                })
            except Exception as e:
                printer_list.append({
                    "name": printer_name,
                    "is_online": False,
                    "status": f"Error: {e}"
                })
        return printer_list

if __name__ == "__main__":  
    test=devices()
    # print(test.get_printer_list_with_status())
    test.checkSupport()
    print(test.support)

    # test.print()